package edu.cmu.cs.stage3.alice.authoringtool.datatransfer;

import java.awt.datatransfer.DataFlavor;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringTool;
import edu.cmu.cs.stage3.alice.authoringtool.AuthoringToolResources;

/**
 * @author Jason Pratt
 */
public class CopyFactoryTransferable implements java.awt.datatransfer.Transferable {

 //LOB version using 'mimeType' to create 'java.awt.datatransfer.DataFlavor' produce error in Java 5, changed to 'Class'
 public static java.awt.datatransfer.DataFlavor copyFactoryFlavor = AuthoringToolResources.getReferenceFlavorForClass( edu.cmu.cs.stage3.alice.core.CopyFactory.class );

 protected edu.cmu.cs.stage3.alice.core.CopyFactory copyFactory;

 protected java.awt.datatransfer.DataFlavor myFlavor;
 protected java.awt.datatransfer.DataFlavor[] flavors;

 public CopyFactoryTransferable( edu.cmu.cs.stage3.alice.core.CopyFactory copyFactory ) {
  this.copyFactory = copyFactory;

  try {
   myFlavor = AuthoringToolResources.getReferenceFlavorForClass( edu.cmu.cs.stage3.alice.core.CopyFactory.class );
   myFlavor.setHumanPresentableName( "copyFactoryTransferable(" + copyFactory.getValueClass().getName() + ")" );
  } catch( Exception e ) {
   AuthoringTool.getInstance().showErrorDialog( e.getMessage(), e );
  }

  flavors = new java.awt.datatransfer.DataFlavor[2];
  flavors[0] = myFlavor;
  flavors[1] = java.awt.datatransfer.DataFlavor.stringFlavor;
 }

 public java.awt.datatransfer.DataFlavor[] getTransferDataFlavors() {
  return flavors;
 }

 public boolean isDataFlavorSupported( java.awt.datatransfer.DataFlavor flavor ) {
  //LOB for (DataFlavor flavor2 : flavors)
  DataFlavor [] flavor2 = flavors;
  int _tam = flavor2!=null ? flavor2.length : 0;
  for (int _i=0; _i<_tam; _i++) { //L
   if( flavor.equals( flavor2[_i] ) ) { //L flavor.equals( flavor2 )
    return true;
   }
  }
  return false;
 }

 public Object getTransferData( java.awt.datatransfer.DataFlavor flavor ) throws java.awt.datatransfer.UnsupportedFlavorException, java.io.IOException {
  if( flavor.getRepresentationClass().equals( edu.cmu.cs.stage3.alice.core.CopyFactory.class ) ) {
   return copyFactory;
  } else if( flavor.equals( java.awt.datatransfer.DataFlavor.stringFlavor ) ) {
   return copyFactory.toString();
  } else {
   throw new java.awt.datatransfer.UnsupportedFlavorException( flavor );
  }
 }

}
